//
//  JCComposeToolBar.swift
//  SinaWeibo_18
//
//  Created by apple on 15/10/26.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

enum JCComposeToolBarButtonType: Int{
    case picture = 0            //图片
     case Mention = 1           //@
     case Trend = 2             //话题
     case Emotion = 3           //表情
     case Add = 4               //加号
}
//声明代理
protocol JCComposeToolBarDelegate: NSObjectProtocol {
    //设置代理方法  (toolbar子控件点击回调的方法  )
    func composeToolBar(toolBar: JCComposeToolBar, buttonClickType: JCComposeToolBarButtonType)
}


class JCComposeToolBar: UIView {
    
    //设置代理
    weak var delegate : JCComposeToolBarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        addChildButton("compose_toolbar_picture",type: .picture)
        addChildButton("compose_mentionbutton_background",type: .Mention)
        addChildButton("compose_trendbutton_background",type: .Trend)
        addChildButton("compose_emoticonbutton_background",type: .Emotion)
        addChildButton("compose_add_background",type: .Add)

    }
    private func addChildButton(imageName: String, type: JCComposeToolBarButtonType){
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        //设置tag值 (枚举的原始值)
        button.tag = type.rawValue
        
        //添加不同状态图片
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        addSubview(button)
    }
    @objc private func childButtonClick(button:UIButton){
        if let del = delegate {
            del.composeToolBar(self, buttonClickType: JCComposeToolBarButtonType(rawValue: button.tag)!)
        }
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let childViewW = width / CGFloat(subviews.count)
        
        for(index,value) in subviews.enumerate(){
            value.size = CGSizeMake(childViewW, height)
            value.x = CGFloat(index) * childViewW
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
